#pragma once
#include <iostream>
#include <stdarg.h>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
enum
{
    Debug = 0,
    Info,
    Warnning,
    Error,
    Fatal
};

enum
{
    screen = 10,
    onefile,
    classfile
};

std::string LevelToString(int level)
{
    switch (level)
    {
    case Debug:
        return "Debug";
    case Info:
        return "Info";
    case Warnning:
        return "Warnning";
    case Error:
        return "Error";
    case Fatal:
        return "Fatal";
    default:
        return "Unknown";
    }
}
const int defaultstyle = screen; // 默认输出消息在显示器上
const std::string defaultfilename = "log.";
const std::string logdir = "log";
//[level][2024-12-19][pid] 消息
// log.message(level,"xxx%sxxx%d\n","xxx",x);
class Log
{
public:
    Log(int style = defaultstyle, std::string filename = defaultfilename) : _style(style), _filename(filename)
    {
        mkdir(logdir.c_str(), 0775);
    }
    void enable(int style)
    {
        if (style <= classfile && style >= screen)
            _style = style;
    }

    std::string TimeStampLocalToTime()
    {
        time_t curr = time(nullptr);
        struct tm *tm = localtime(&curr);
        char time_buffer[128];
        snprintf(time_buffer, sizeof(time_buffer), "%d-%d-%d %d:%d:%d",
                 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
                 tm->tm_hour, tm->tm_min, tm->tm_sec);
        return time_buffer;
    }

    void writetoonefile(const std::string &logname, const std::string &loginfo)
    {
        umask(0);
        int fd = open(logname.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666);
        if (fd < 0)
        {
            return;
        }
        write(fd, loginfo.c_str(), loginfo.size());
        close(fd);
    }

    void writelogtoclassfile(const std::string strlevel, const std::string loginfo)
    {
        // log/log.info
        std::string logname = logdir;
        logname += "/";
        logname += _filename;
        logname += strlevel;
        writetoonefile(logname, loginfo);
    }
    void writelog(const std::string &strlevel, const std::string &loginfo)
    {
        switch (_style)
        {
        case screen:
            std::cout << loginfo;
            break;
        case onefile:
            writelogtoclassfile("all", loginfo);
            break;
        case classfile:
            writelogtoclassfile(strlevel.c_str(), loginfo);
        default:
            break;
        }
    }

    void logmessage(int level, const char *format, ...)
    {
        char leftbuffer[1024];
        std::string strlevel = LevelToString(level);
        std::string currtime = TimeStampLocalToTime();
        std::string strid = std::to_string(getpid());
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%s][%s] ", strlevel.c_str(), currtime.c_str(), strid.c_str());

        va_list args;
        va_start(args, format);
        char rightbuffer[1024];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, args);
        va_end(args);

        std::string loginfo = leftbuffer;
        loginfo += rightbuffer;
        loginfo += "\n";

        writelog(strlevel, loginfo);
    }

    ~Log()
    {
    }

private:
    int _style;
    std::string _filename;
};

Log lg;

class Conf
{
public:
    Conf(int style = screen) : _style(style)
    {
        lg.enable(style);
    }
    ~Conf()
    {
    }

private:
    int _style;
};

Conf cnf;